#ifndef BSP_UART_H
#define BSP_UART_H
#include "hc32_ddl.h"
#define RXLEN 1024u

#define USART1_RX_PORT       PortD
#define USART1_RX_PIN        Pin02

#define USART2_RX_PORT       PortC//PortC
#define USART2_RX_PIN        Pin01//Pin12
#define USART3_RX_PORT       PortE
#define USART3_RX_PIN        Pin04
#define USART4_RX_PORT       PortB
#define USART4_RX_PIN        Pin07

#define USART1_TX_PORT       PortD//PortB
#define USART1_TX_PIN        Pin01//Pin03//Pin15

#define USART2_TX_PORT       PortC//PortD
#define USART2_TX_PIN        Pin00//Pin00
#define USART3_TX_PORT       PortE//PortB
#define USART3_TX_PIN        Pin05//Pin15
#define USART4_TX_PORT       PortB//PortB
#define USART4_TX_PIN        Pin06//Pin15

#define USART1_RX_FUNC       Func_Usart1_Rx
#define USART1_TX_FUNC       Func_Usart1_Tx

#define USART2_RX_FUNC       Func_Usart2_Rx
#define USART2_TX_FUNC       Func_Usart2_Tx

#define USART3_RX_FUNC       Func_Usart3_Rx
#define USART3_TX_FUNC       Func_Usart3_Tx

#define USART4_RX_FUNC       Func_Usart4_Rx
#define USART4_TX_FUNC       Func_Usart4_Tx

#define USART1_RI_NUM        INT_USART1_RI
#define USART1_EI_NUM        INT_USART1_EI
#define USART1_TI_NUM        INT_USART1_TI

#define USART2_RI_NUM        INT_USART2_RI
#define USART2_EI_NUM        INT_USART2_EI
#define USART2_TI_NUM        INT_USART2_TI

#define USART3_RI_NUM        INT_USART3_RI
#define USART3_EI_NUM        INT_USART3_EI
#define USART3_TI_NUM        INT_USART3_TI

#define USART4_RI_NUM        INT_USART4_RI
#define USART4_EI_NUM        INT_USART4_EI
#define USART4_TI_NUM        INT_USART4_TI

#define USART1_CLK           PWC_FCG1_PERIPH_USART1
#define USART2_CLK           PWC_FCG1_PERIPH_USART2
#define USART3_CLK           PWC_FCG1_PERIPH_USART3
#define USART4_CLK           PWC_FCG1_PERIPH_USART4


#define UART1_DMA_UNIT			M4_DMA2
#define UART1_DMA_RX_CH			(DmaCh3)
#define UART1_DMA_TX_CH			(DmaCh2)
#define UART1_DMA_RX_TRG		EVT_USART1_RI
#define UART1_DMA_TX_TRG		EVT_USART1_TI
#define UART1_DMA_RX_INT		INT_DMA2_TC3
#define UART1_DMA_TX_INT		INT_DMA2_TC2

#define UART4_DMA_UNIT			M4_DMA2
#define UART4_DMA_RX_CH			(DmaCh0)
#define UART4_DMA_TX_CH			(DmaCh1)
#define UART4_DMA_RX_TRG		EVT_USART4_RI
#define UART4_DMA_TX_TRG		EVT_USART4_TI
#define UART4_DMA_RX_INT		INT_DMA2_TC0
#define UART4_DMA_TX_INT		INT_DMA2_TC1


#define UART2_DMA_UNIT			M4_DMA1
#define UART2_DMA_RX_CH			(DmaCh3)
#define UART2_DMA_TX_CH			(DmaCh2)
#define UART2_DMA_RX_TRG		EVT_USART2_RI
#define UART2_DMA_TX_TRG		EVT_USART2_TI
#define UART2_DMA_RX_INT		INT_DMA1_TC3
#define UART2_DMA_TX_INT		INT_DMA1_TC2

#define UART3_DMA_UNIT			M4_DMA1
#define UART3_DMA_RX_CH			(DmaCh0)
#define UART3_DMA_TX_CH			(DmaCh1)
#define UART3_DMA_RX_TRG		EVT_USART3_RI
#define UART3_DMA_TX_TRG		EVT_USART3_TI
#define UART3_DMA_RX_INT		INT_DMA1_TC0
#define UART3_DMA_TX_INT		INT_DMA1_TC1



typedef struct
{
    stc_usart_uart_init_t stcUartConf;
    uint8_t RTO_enable;
    en_port_t rxPort;
    uint16_t rxPin;
    en_port_func_t rxfunc;
    en_port_t txPort;
    uint16_t txPin;
    en_port_func_t txfunc;
    uint32_t Baudrate;
}bsp_uart_cfg_t;



#ifdef __cplusplus
extern "C" {
#endif

extern uint8_t receive_flag3;
extern uint16_t receive_len3;
extern uint8_t receive_buffer3[RXLEN];

extern uint8_t receive_flag4;
extern uint16_t receive_len4;
extern uint8_t receive_buffer4[RXLEN];

extern uint8_t receive_flag1;
extern uint16_t receive_len1;
extern uint8_t receive_buffer1[RXLEN];

extern uint8_t receive_flag2;
extern uint16_t receive_len2;
extern uint8_t receive_buffer2[RXLEN];

uint8_t bsp_uart_init(M4_USART_TypeDef *USARTx,bsp_uart_cfg_t* uart_cfg);
uint8_t bsp_uart1_init(void);
uint8_t bsp_uart2_init(void);
uint8_t bsp_uart3_init(void);
uint8_t bsp_uart4_init(void); 
void bsp_uart1_transfer_dma(uint8_t *data, uint16_t len);
void bsp_uart2_transfer_dma(uint8_t *data, uint16_t len);
void bsp_uart3_transfer_dma(uint8_t *data, uint16_t len);
void bsp_uart4_transfer_dma(uint8_t *data, uint16_t len);
#ifdef __cplusplus
};
#endif

#endif

